using System.Transactions;
using OracleInternal.Common;

namespace OracleInternal.MTS
{
	internal class MTSProxy
	{
		private class MTSTxnRMPool : SyncDictionary<string, MTSTxnRM>
		{
		}

		private MTSTxnRMPool m_CCPRMs = new MTSTxnRMPool();

		private MTSTxnRMPool m_OCPRMs = new MTSTxnRMPool();

		private string m_dbEasyConnectName = string.Empty;

		private object m_lock = new object();

		internal MTSProxy(string easyConnectName)
		{
			m_dbEasyConnectName = easyConnectName;
		}

		internal MTSTxnRM GetRM(bool bIsCCP, string serviceName, string pdbName, Transaction txn)
		{
			MTSTxnRM mTSTxnRM = null;
			MTSTxnRMPool mTSTxnRMPool = null;
			string localIdentifier = txn.TransactionInformation.LocalIdentifier;
			mTSTxnRMPool = m_CCPRMs;
			if ((mTSTxnRM = mTSTxnRMPool[localIdentifier]) == null)
			{
				lock (m_lock)
				{
					if ((mTSTxnRM = mTSTxnRMPool[localIdentifier]) != null)
					{
						return mTSTxnRM;
					}
					MTSTxnRM mTSTxnRM2 = (mTSTxnRMPool[localIdentifier] = MTSTxnRMCache.GetRM(bIsCCP));
					mTSTxnRM = mTSTxnRM2;
					mTSTxnRM.Initialize(m_dbEasyConnectName, serviceName, pdbName, txn);
					return mTSTxnRM;
				}
			}
			return mTSTxnRM;
		}

		internal void RemoveRM(bool bIsCCP, string txnLocalID)
		{
			MTSTxnRM mTSTxnRM = null;
			MTSTxnRMPool mTSTxnRMPool = null;
			mTSTxnRMPool = m_CCPRMs;
			if ((mTSTxnRM = mTSTxnRMPool[txnLocalID]) != null)
			{
				mTSTxnRMPool.Remove(txnLocalID);
				mTSTxnRM.Reset();
				MTSTxnRMCache.PutRM(mTSTxnRM);
			}
		}
	}
}
